package com.jmlee.service.impl;

import com.jmlee.controller.WebSocketServerEndpoint;
import com.jmlee.mapper.OrderMapper;
import com.jmlee.mapper.RiderMapper;
import com.jmlee.pojo.Rider;
import com.jmlee.service.RiderService;
import com.jmlee.utils.CheckInCircle;
import com.jmlee.vo.AddressByOrderVo;
import com.jmlee.vo.ChangePwdVo;
import com.jmlee.vo.GetRiderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

/**
 * @author： 郑锦涛
 * @data： 2021/3/8 16:24
 * @version：1.0
 * @描述： RiderService实现类
 */
@Service
public class RiderServiceImpl implements RiderService
{

    @Autowired
    RiderMapper riderMapper;

    @Autowired
    OrderMapper orderMapper;

    @Override
    public Rider riderLogin(Rider rider) {
        return riderMapper.selectRiderByLogin(rider);
    }

    @Override
    public List<AddressByOrderVo> getOrderAddress(GetRiderVo getRiderVo) {
        return orderMapper.getOrderAddress(getRiderVo);
    }

    @Override
    public Rider changeRiderInfo(Rider rider) {
        //修改数据骑手数据信息
        Integer change = riderMapper.changeRiderInfo(rider);
        //判断是否修改成功
        if (change>0){
            return riderMapper.selectRiderByChange(rider);
        }
        return null;
    }

    @Override
    public Rider changePwd(ChangePwdVo changePwdVo, Rider rider) {
        //修改密码
        Integer change = riderMapper.changePwd(changePwdVo);
        //判断是否修改成功，成功则查询返回修改后的骑手数据
        if (change > 0) {
            return riderMapper.selectRiderByChange(rider);
        }
        return null;
    }

    @Override
    public List<AddressByOrderVo> takeOrder(GetRiderVo getRiderVo) {
        Integer take = orderMapper.updateOrderRiderId(getRiderVo);
        if (take>0){
            //获取已付款状态的订单
            GetRiderVo getRider = new GetRiderVo();
            getRider.setOrderStatus(20);
            List<AddressByOrderVo> list = CheckInCircle.inCircleOrder(getOrderAddress(getRider),WebSocketServerEndpoint.addressByRiderVo);
            return list;
        }
        return null;
    }

    @Override
    public List<AddressByOrderVo> pickUp(GetRiderVo getRiderVo) {
        Integer pick = orderMapper.updateOrderStatus(getRiderVo);
        if (pick>0){
            GetRiderVo riderVo = new GetRiderVo();
            riderVo.setOrderStatus(20);
            riderVo.setUserName(getRiderVo.getUserName());
            riderVo.setOpenid(getRiderVo.getOpenid());
            return orderMapper.getOrderAddress(riderVo);
        }

        return null;
    }

    @Override
    public List<AddressByOrderVo> orderOver(GetRiderVo getRiderVo) {
        //判断状态是否改变
        Integer over = orderMapper.updateOrderStatus(getRiderVo);
        if (over>0){
            GetRiderVo riderVo = new GetRiderVo();
            riderVo.setOrderStatus(30);
            riderVo.setUserName(getRiderVo.getUserName());
            riderVo.setOpenid(getRiderVo.getOpenid());
            return orderMapper.getOrderAddress(riderVo);
        }
        return null;
    }

    @Override
    public AddressByOrderVo goEarth(GetRiderVo getRiderVo) {
        AddressByOrderVo address = new AddressByOrderVo();
        List<AddressByOrderVo> list = orderMapper.getOrderAddress(getRiderVo);
        //通过迭代获取其中的数据
        Iterator it = list.iterator();
        while (it.hasNext()){
            address = (AddressByOrderVo) it.next();
        }

        return address;
    }

    @Override
    public boolean updateIdCard(Rider rider) {
        Integer idCard = riderMapper.changeRiderInfo(rider);
        if (idCard>0){
            return true;
        }
        return false;
    }

    @Override
    public Rider getChangeInfo(Rider rider) {
        return riderMapper.selectRiderByChange(rider);
    }

    @Override
    public Rider regByOpenid(Rider rider) {
        Integer reg = riderMapper.insertRider(rider);
        if (reg > 0){
            return riderMapper.selectRiderByChange(rider);
        }
        return null;
    }

    @Override
    public Integer regByUsername(Rider rider) {
        return riderMapper.insertRider(rider);
    }

    @Override
    public List<AddressByOrderVo> chooseOrder(GetRiderVo getRiderVo) {
        return orderMapper.chooseOrder(getRiderVo);
    }
}
